package webServiceClient.academico;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class FrmCurso extends Activity {
	private Button btnCad;
	private Button btnFechar;
	private EditText edtCodCurso;
	private EditText edtNomCurso;
	private CheckBox cbCurDisp;

	private void limpaEdt(){
		edtCodCurso.setText("");
		edtNomCurso.setText("");
		edtCodCurso.requestFocus();
	}
	private void carregaBtn() {
		btnCad = (Button) findViewById(R.FrmCurso.btnCad);
		btnFechar = (Button) findViewById(R.FrmCurso.btnFechar);
		edtCodCurso = (EditText) findViewById(R.FrmCurso.edtCodCurso);
		edtNomCurso = (EditText) findViewById(R.FrmCurso.edtNomCurso);
		cbCurDisp = (CheckBox) findViewById(R.FrmCurso.cbCursoDisp);

		btnFechar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		btnCad.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int disp = 0;
				if (cbCurDisp.isChecked() == true)
					disp = 1;
				try {
					WebServiceClient ws = new WebServiceClient();
					String s = ws.addCurso("addCurso", Definicoes.getRaAdm(),
							Definicoes.getSenhaAdm(),
							edtCodCurso.getText().toString(),
							edtNomCurso.getText().toString(), disp);
					if (s.equals("true")) {
						Toast t = Toast.makeText(FrmCurso.this,
								"Curso cadastrado com sucesso",
								Toast.LENGTH_LONG);
						t.show();
						limpaEdt();
					}else
					{
						Toast t = Toast.makeText(FrmCurso.this,
								"Erro ao cadastrar curso - "+s,
								Toast.LENGTH_LONG);
						t.show();						
					}
				} catch (Exception e) {
					Toast t = Toast.makeText(FrmCurso.this,
							Definicoes._erroServer,
							Toast.LENGTH_LONG);
					t.show();
				}
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frm_curso);
		carregaBtn();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.frm_curso, menu);
		return true;
	}

}
